package com.yu8home.opcua.test;

import com.yu8home.opcua.OpcUAUtil;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedDataItem;
import org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedEventItem;
import org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription;
import org.eclipse.milo.opcua.stack.core.types.builtin.NodeId;
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant;
import org.eclipse.milo.opcua.stack.core.types.structured.EventFilter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

@Slf4j
public class BatchSub {

    public static void main(String[] args) throws Exception {
        OpcUAUtil.getConnect();
        // createSubscribe();
        // createSubscribe2();
        createSubscribe3();
        System.in.read();
    }


    public static void createSubscribe() throws Exception {
        List<NodeId> nodeIds = new ArrayList<>();
        for (String s : Const.subscribes) {
            nodeIds.add(new NodeId(OpcUAUtil.namespaceIndex, s));
        }
        ManagedSubscription subscription = ManagedSubscription.create(OpcUAUtil.opcUaClient);
        List<ManagedDataItem> dataItems = subscription.createDataItems(nodeIds);
        for (ManagedDataItem t : dataItems) {
            t.addDataValueListener(f -> {
                log.info("封装订阅：{} = {}", t.getNodeId().getIdentifier(), f.getValue().getValue());
            });
        }
    }


    public static void createSubscribe2() throws Exception {
        List<NodeId> nodeIds = new ArrayList<>();
        List<EventFilter> eventFilters = new ArrayList<>();

        for (String s : Const.subscribes) {
            nodeIds.add(new NodeId(OpcUAUtil.namespaceIndex, s));
            eventFilters.add(Const.eventFilter);
        }
        ManagedSubscription subscription = ManagedSubscription.create(OpcUAUtil.opcUaClient);
        List<ManagedEventItem> eventItems = subscription.createEventItems(nodeIds, eventFilters);
        for (ManagedEventItem eventItem : eventItems) {
            eventItem.addEventValueListener(f -> {
                log.info("=====identifier：{}", eventItem.getNodeId().getIdentifier());
                for (Variant variant : f) {
                    log.info("=====value：{}", variant.getValue());
                }
            });
        }
    }


    public static void createSubscribe3() throws Exception {
        List<NodeId> nodeIds = new ArrayList<>();
        List<EventFilter> eventFilters = new ArrayList<>();

        for (String s : Const.subscribes) {
            nodeIds.add(new NodeId(OpcUAUtil.namespaceIndex, s));
            eventFilters.add(Const.eventFilter);
        }
        ManagedSubscription subscription = ManagedSubscription.create(OpcUAUtil.opcUaClient);
        List<ManagedEventItem> eventItems = subscription.createEventItems(nodeIds, eventFilters);

        final CountDownLatch eventLatch = new CountDownLatch(7);
        Thread.sleep(10 * 1000);

        subscription.addEventChangeListener((_eventItems, variants) -> {
            for (int i = 0; i < _eventItems.size(); i++) {
                ManagedEventItem eventItem = _eventItems.get(i);
                Variant[] eventFieldValues = variants.get(i);

                log.info("标识符：{}", eventItem.getNodeId().getIdentifier().toString());

                for (int j = 0; j < eventFieldValues.length; j++) {
                    log.info("变量variant[{}]: {}", j, eventFieldValues[j].getValue());
                }
            }
            eventLatch.countDown();
        });
        eventLatch.await();
    }

}
